﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Asd.Models;
using Asd.Reposiroty;
using Asd.Request;

namespace Asd.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            var schoolRep = new EfRepository<School>();
            var schools = schoolRep.Select.FirstOrDefault();
            if (schools == null)
            {
                schoolRep.Insert(new School
                {
                    SchoolName = "闽大"
                });
            }

            var stuRep = new EfRepository<Student>();
            var stus = stuRep.Select.Where(x => x.IsDeleted == false).ToList();
            return View(stus);
        }

        public IActionResult Delete(int id)
        {
            var stuRep = new EfRepository<Student>();
            var stus = stuRep.GetById(id);
            if (stus != null)
            {
                stuRep.Delete(id);
            }
            return RedirectToAction("Index");
        }

        public IActionResult Save(StuRequest model)
        {
            var stuRep = new EfRepository<Student>();
            if (model.Id > 0)
            {
                var stus = stuRep.GetById(model.Id);
                if (stus != null)
                {
                    stus.StudnetName = model.StudentName;
                    stus.Remarks = model.Remarks;

                    stuRep.Update(stus);
                }
            }
            else
            {
                stuRep.Insert(new Student
                {
                    StudnetName = model.StudentName,
                    Remarks = model.Remarks
                });
            }
            return Ok();
        }

        public IActionResult Query(QueryRequest model)
        {
            var stuRep = new EfRepository<Student>();
            var stus = stuRep.Select.Where(x => x.IsDeleted == false).ToList();
            var keyword = model.keyword != null ? model.keyword.Trim() : "";
            if (keyword.Length > 0)
            {
                stus = stuRep.Select.Where(x => x.IsDeleted == false && (x.StudnetName.Contains(keyword) || x.Remarks.Contains(keyword))).ToList();
            }
            return Json(stus);
        }

        public IActionResult CreateOrEdit(int id)
        {
            var stuRep = new EfRepository<Student>();
            var stus = stuRep.GetById(id);
            return View(stus);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
